#include "shape_dll.h"

#include <QDebug>
#include <QFile>
#include <QDir>
#include <QTextStream>
#include <QString>
#include <QList>
#include <cmath>


Circle::Circle(double _rad) { x = 0; y = 0; rad1 = _rad; rad2 = _rad;}
Circle::Circle(double _x, double _y, double _rad, double _angel)
    {
        x = _x;
        y = _y;
        rad1 = _rad;
        rad2 = _rad;
        angel = _angel;
    }
QString Circle::Print_type()
    {
        QString type = "CIRCLE";
        return type;
    }
double Circle::square()
    {
        double sq;
        sq = round (3.1415269 * rad1*rad1*100)/100;
        return sq;
    }
double Circle::perimetr()
    {
        double per;
        per = round(2 * 3.1415269*rad1*100)/100;
        return per;
    }
void Circle::moveShape(double _x, double _y)
    {
        x =_x;
        y =_y;
    }
void Circle::shiftShape(double _angel)
{
    angel = angel + _angel;
}

QList<QString> Circle::getInf()
    {
        QList<QString>inf {QString::number(x),QString::number(y),QString::number(rad1), QString::number(angel)};
        return inf;
    }


Triangle::Triangle(double _a, double _b) { x = 0; y = 0; a = _a; b = _b; }
Triangle::Triangle(double _x, double _y, double _a, double _b, double _angel){
    x =_x;
    y =_y;
    a =_a;
    b =_b;
    angel = _angel;
}
double Triangle::perimetr()
    {
        double per;
        per = round((a + b + sqrt(a*a + b*b))*100)/100;
        return per;
    }
double Triangle::square()
    {
        double sq;
        sq = round(0.5*a*b*100)/100;
        return sq;
    }
QString Triangle::Print_type()
    {
        QString type = "TRIANGLE";
        return type;
    }
void Triangle::moveShape(double _x, double _y)
    {
        x=_x;
        y=_y;
    }
void Triangle::shiftShape(double _angel)
{
    angel = angel+_angel;
}

QList<QString> Triangle::getInf()
    {
        QList<QString>inf {QString::number(x),QString::number(y),QString::number(a),QString::number(b),QString::number(angel) };
        return inf;
    }


Rectangle::Rectangle(double _a, double _b) { x=0; y=0; a = _a; b = _b; }
Rectangle::Rectangle(double _x, double _y, double _a, double _b, double _angel){
    x=_x;
    y=_y;
    a=_a;
    b=_b;
    angel = _angel;
}
double Rectangle::perimetr()
    {
        double per;
        per = round(2 * a + 2 * b*100)/100;
        return per;
    }
double Rectangle::square()
    {
        double sq;
        sq = round(a*b*100)/100;
        return sq;
    }
QString Rectangle::Print_type()
    {
        QString type = "RECTANGLE";
        return type;
    }
void Rectangle::moveShape(double _x, double _y)
    {
        x=_x;
        y=_y;
    }
void Rectangle::shiftShape(double _angel)
{
    angel = angel+_angel;
}
QList<QString> Rectangle::getInf()
    {
        QList<QString>inf {QString::number(x),QString::number(y),QString::number(a),QString::number(b), QString::number(angel)};
        return inf;
    }


ShapeConteiner::ShapeConteiner(){}
ShapeConteiner::~ShapeConteiner()
{
    for(QVector<Shape*>::iterator it = mass.begin(); it != mass.end(); ++it)
    {
        delete (*it);
    }
}
int ShapeConteiner:: size()
{
    return mass.size();
}
void ShapeConteiner::InsertFig(Shape*fig)
{
    mass.push_back(fig);
}
void ShapeConteiner::RemoveFig(int i)
{
    delete mass[i];
    mass.erase(mass.begin() + i);
}
Shape* ShapeConteiner::operator[](int i)
{
    return mass[i];
}
void ShapeConteiner::Change(int index, double _x, double _y)
{
    mass[index]->moveShape(_x, _y);
}
void ShapeConteiner::writeToFile(QString fileName)
{
    QFile file(fileName);
    if (!file.open(QFile::WriteOnly | QFile::Text))
    {
       return;
    }
    QTextStream stream(&file);
    for (int i = 0; i < mass.size(); i++)
    {
        stream << mass[i]->Print_type() << endl;
        QList<QString> inf = mass[i]->getInf() ;
        for(int j = 0; j<inf.size(); j++)
        {
            stream << inf[j] << endl;
        }
    }
    file.close();
}
void ShapeConteiner::readFromFile(QString fileName)
 {
     QFile file(fileName);
     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
     {
         qDebug() <<"erorr";
            return;
     }

     QTextStream in(&file);
     while (!in.atEnd())
     {
         QString type = in.readLine();
         if (type == "CIRCLE")
         {
             double x = in.readLine().toDouble();
             double y = in.readLine().toDouble();
             double rad = in.readLine().toDouble();
             double angel = in.readLine().toDouble();
             Circle *fig = new Circle(x, y, rad, angel);
             InsertFig(fig);
         }
         else if (type == "TRIANGLE")
         {
             double x = in.readLine().toDouble();
             double y = in.readLine().toDouble();
             double a = in.readLine().toDouble();
             double b = in.readLine().toDouble();
            // double c = in.readLine().toDouble();
             double angel = in.readLine().toDouble();
             Triangle *fig = new Triangle(x, y, a, b, angel);
             InsertFig(fig);
         }
         else if (type == "RECTANGLE")
         {
             double x = in.readLine().toDouble();
             double y = in.readLine().toDouble();
             double a = in.readLine().toDouble();
             double b = in.readLine().toDouble();
             double angel = in.readLine().toDouble();
             Rectangle *fig = new Rectangle(x, y, a, b, angel);
             InsertFig(fig);
         }
     }
     file.close();
 }



